﻿using Newtonsoft.Json;
using Swsk33.FinalCourseDesign.DataObject;
using Swsk33.FinalCourseDesign.Model;
using Swsk33.ReadAndWriteSharp.Network;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;

namespace Swsk33.FinalCourseDesign.Frame
{
	public partial class SelectDialog : Form
	{
		/// <summary>
		/// 鼠标按下时在窗口中的x坐标
		/// </summary>
		private int mouseAtX;

		/// <summary>
		/// 鼠标按下时在窗口中的y坐标
		/// </summary>
		private int mouseAtY;

		/// <summary>
		/// 鼠标是否在窗口上是按下状态
		/// </summary>
		private bool isMouseDown = false;

		/// <summary>
		/// 被选择餐厅列表
		/// </summary>
		private List<Canteen> selectCanteenList;

		/// <summary>
		/// 在列表中被选中的餐厅索引
		/// </summary>
		private int selectCanteenIndex;

		/// <summary>
		/// 被选择门店类型
		/// </summary>
		private string type;

		/// <summary>
		/// 被选择餐厅列表
		/// </summary>
		public List<Canteen> SelectCanteenList { get => selectCanteenList; set => selectCanteenList = value; }

		/// <summary>
		/// 被选中餐厅索引
		/// </summary>
		public int SelectCanteenIndex { get => selectCanteenIndex; set => selectCanteenIndex = value; }

		/// <summary>
		/// 被选择门店类型
		/// </summary>
		public string Type { get => type; set => type = value; }

		public SelectDialog()
		{
			CheckForIllegalCrossThreadCalls = false;
			InitializeComponent();
		}

		/// <summary>
		/// 根据条件查询食堂
		/// </summary>
		private void queryCanteen()
		{
			// 设定控件
			tip.Visible = true;
			canteenBox.Enabled = false;
			canteenBox.Items.Clear();
			ok.Enabled = false;
			Application.DoEvents();
			// 组装url
			string url;
			int min = 0;
			int max = 0;
			if (priceBox.SelectedIndex == 0)
			{
				url = Configure.GetConfigure().Server + "/getcanteenbytype/" + typeBox.SelectedItem.ToString();
			}
			else
			{
				switch (priceBox.SelectedIndex)
				{
					case 1:
						min = 0;
						max = 9;
						break;
					case 2:
						min = 9;
						max = 12;
						break;
					case 3:
						min = 12;
						max = 15;
						break;
					case 4:
						min = 15;
						max = 100;
						break;
					default:
						min = 15;
						max = 100;
						break;
				}
				url = Configure.GetConfigure().Server + "/getcanteenbytypeandprice/" + typeBox.SelectedItem.ToString() + "/" + min + "/" + max;
			}
			// 异步发送请求
			new Thread(() =>
			{
				string requestBody = null;
				try
				{
					requestBody = NetworkUtils.SendGetRequest(url);
				}
				catch (Exception)
				{
					MessageBox.Show("请求失败！请检查网络再试！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
					DialogResult = DialogResult.Cancel;
					Close();
					return;
				}
				Result<List<Canteen>> result = JsonConvert.DeserializeObject<Result<List<Canteen>>>(requestBody);
				if (result.Success)
				{
					SelectCanteenList = result.Data;
					// 设定控件
					tip.Visible = false;
					canteenBox.Enabled = true;
					string[] names = new string[SelectCanteenList.Count];
					for (int i = 0; i < SelectCanteenList.Count; i++)
					{
						names[i] = SelectCanteenList[i].Name;
					}
					canteenBox.Items.AddRange(names);
					canteenBox.SelectedIndex = 0;
					ok.Enabled = true;
					Application.DoEvents();
				}
				else
				{
					MessageBox.Show("获取失败！请重新设定条件！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}).Start();
		}

		private void SelectDialog_MouseDown(object sender, MouseEventArgs e)
		{
			Cursor = Cursors.SizeAll;
			mouseAtX = e.X;
			mouseAtY = e.Y;
			isMouseDown = true;
		}

		private void SelectDialog_MouseMove(object sender, MouseEventArgs e)
		{
			if (isMouseDown)
			{
				Left = MousePosition.X - mouseAtX;
				Top = MousePosition.Y - mouseAtY;
			}
		}

		private void SelectDialog_MouseUp(object sender, MouseEventArgs e)
		{
			Cursor = Cursors.Default;
			isMouseDown = false;
		}

		private void close_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			Close();
		}

		private void SelectDialog_Load(object sender, EventArgs e)
		{
			typeBox.SelectedIndex = 0;
			priceBox.SelectedIndex = 0;
			queryCanteen();
			// 后面再加上方法监听防止产生重复
			typeBox.SelectedIndexChanged += new EventHandler(this.typeAndPriceBoxChanged);
			priceBox.SelectedIndexChanged += new EventHandler(this.typeAndPriceBoxChanged);
		}

		private void typeAndPriceBoxChanged(object sender, EventArgs e)
		{
			queryCanteen();
		}

		private void ok_Click(object sender, EventArgs e)
		{
			// 设定相关数据
			SelectCanteenIndex = canteenBox.SelectedIndex;
			Type = typeBox.SelectedItem.ToString();
			DialogResult = DialogResult.OK;
		}
	}
}